/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tsb.xmlparser;

import csm.uttils.generics.Roster;
import java.util.HashMap;
import javax.swing.tree.DefaultMutableTreeNode;

/**
 *
 * @author Carlos S. Moreno <CarlosS.Moreno@gmail.com>
 */
public class Node extends DefaultMutableTreeNode implements Comparable {

    private String name, value;
    private Roster<Node> nodes;
    private HashMap<String, String> properties;
    private static int newID;
    private int id;

    public Node() {
        startUp();
    }

    public Node(String name, String value) {
        this.name = name;
        this.value = value;
        startUp();
    }

    private void startUp() {
        setNodes(new Roster<Node>());
        properties = new HashMap<String, String>();
        id = ++newID;
    }

    public int compareTo(Object o) {
        if (o.getClass().getCanonicalName().compareToIgnoreCase("Node") != 0) {
            return -1;
        } else {
            return id - ((Node) o).getNodeID();
        }
    }

    public int getNodeID() {
        return id;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the value
     */
    public String getValue() {
        return value;
    }

    /**
     * @param value the value to set
     */
    public void setValue(String value) {
        this.value = value;
    }

    /**
     * @return the nodes
     */
    public Roster<Node> getNodes() {
        return nodes;
    }

    /**
     * @param nodes the nodes to set
     */
    public void setNodes(Roster<Node> nodes) {
        this.nodes = nodes;
    }

    public void addNode(Node node) {
        getNodes().addInOrder(node);
    }

    public void addPropertie(String name, String value) {
        properties.put(name, value);
    }

    /**
     * @return the properties
     */
    public HashMap<String, String> getProperties() {
        return properties;
    }

    public static void restartIDs()
    {
        newID = 0;
    }

    public String toString() {
        if (null != name) {
            if (null != value) {
                return name + ": " + value;
            } else {
                return name;
            }
        } else {
            return "Undefined";
        }
    }
}
